// 购物车状态

import { mergeLocalCart, findCartList, insertCart, deleteCart, updateCart, checkAllCart } from '@/api/cart'

export default {
  namespaced: true,
  state: () => ({
    list: []
  }),
  getters: {
    // 有效商品
    validList: state => {
      return state.list.filter(item => item.isEffective)
    },
    // 选中商品
    selectList: (state, getter) => {
      return getter.validList.filter(item => item.selected)
    },
    // 总价
    totalPrice: (state, getter) => {
      return getter.selectList.reduce((acc, goods) => (acc += goods.count * goods.nowPrice), 0)
    },
    // 全选
    isAll: (state, getter) => {
      return getter.validList.every(item => item.selected)
    }
  },
  mutations: {
    setCartList (state, list) {
      state.list = list
    },
    addGoods (state, goods) {
      const currentIndex = state.list.findIndex(item => item.skuId === goods.skuId)
      if (currentIndex > -1) {
        // 存在该商品
        state.list[currentIndex].count += goods.count
      } else {
        // 不存在
        state.list.unshift(goods)
      }
    },
    // 修改单选
    singcheck (state, { data, select }) {
      const current = state.list.find(item => item.skuId === data.skuId)
      current.selected = select
    },
    // 修改全选
    allCheck (state, select) {
      state.list.forEach(item => (item.selected = select))
    },
    // 删除商品
    delGoods (state, skuId) {
      const index = state.list.findIndex(item => item.skuId === skuId)
      state.list.splice(index, 1)
    },
    // 修改数量
    editNum (state, { goods, num }) {
      const currgoods = state.list.find(item => item.skuId === goods.skuId)
      currgoods.count = num
    }
  },
  actions: {
    // 获取后台购物车
    async getCartListAction ({ commit }) {
      const { result } = await findCartList()
      commit('setCartList', result)
    },
    // 合并购物车
    async mergeCartAction ({ state, dispatch }) {
      if (state.list.length) {
        const list = state.list.map(({ skuId, selected, count }) => {
          return {
            skuId,
            selected,
            count
          }
        })
        await mergeLocalCart(list)
      }
      dispatch('getCartListAction')
    },
    // 添加购物车
    async addGoodsAction ({ commit, rootState, dispatch }, goods) {
      // 判断是否登陆
      if (rootState.user.profile.token) {
        // 登录
        await insertCart(goods)
        dispatch('getCartListAction')
      } else {
        // 未登录
        commit('addGoods', goods)
      }
    },
    // 单选按钮
    async singleCheckAction ({ commit, rootState, dispatch }, { data, select }) {
      if (rootState.user.profile.token) {
        // 调用接口
        await dispatch('editNumandSelect', { ...data, selected: select })
        await dispatch('getCartListAction')
      } else {
        commit('singcheck', { data, select })
      }
    },

    async allCheckAction ({ commit, rootState, getters, dispatch }, select) {
      if (rootState.user.profile.token) {
        // 调用接口
        const ids = getters.validList.map(({ skuId }) => {
          return skuId
        })
        await checkAllCart({ selected: select, ids })
        dispatch('getCartListAction')
      } else { commit('allCheck', select) }
    },
    // 删除商品
    async delGoodsAction ({ commit, rootState, dispatch }, { skuId }) {
      if (rootState.user.profile.token) {
        // 调用接口
        await deleteCart([skuId])
        dispatch('getCartListAction')
      } else { commit('delGoods', skuId) }
    },
    // 修改数量
    async editNumAction ({ commit, rootState, dispatch }, { goods, num }) {
      if (rootState.user.profile.token) {
        // 调用接口
        await dispatch('editNumandSelect', { ...goods, count: num })
        await dispatch('getCartListAction')
      } else { commit('editNum', { goods, num }) }
    },

    // 修改选中和数量接口
    async editNumandSelect (context, goods) {
      await updateCart(goods)
    }
  }
}
